﻿using BookStore.Application.User;
using BookStore.Web.Areas.Api.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace BookStore.Web.Areas.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UsersController : ApiController
    {
        private readonly IUserAppService _userAppService;

        public UsersController(IUserAppService userAppService)
        {
            _userAppService = userAppService;
        }

        [HttpGet("{userNo}")]
        public async Task<IActionResult> Get(string userNo)
        {
            var dto = await _userAppService.GetAsync(userNo);
            return Ok(dto);
        }

        [HttpPost("Login")]
        public async Task<IActionResult> Login(LoginRequestModel loginRequest)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(loginRequest);
            }
            var loginUser = await _userAppService.GetAsync(loginRequest.UserName, loginRequest.Password);
            if (loginUser != null)
                return Ok(loginUser);
            return BadRequest(loginUser);
        }
    }
}
